import { Request, Response, NextFunction } from 'express';
import _ from 'lodash';
import { getNotification, createNotice, readNotification, updateNotice, deleteNotice, getIdNotification, noticeStatusChange } from './notification.service';
import { getInfoByIdAndType } from '../corner/common.service';

/**
 * 获取内容
 */
export const index = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  let status = request.query.status;

  try {
    const data = await getNotification(parseInt(`${status}`, 10));

    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 获取内容(按 ID 查找)
 */
export const notificationDetail = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容 ID
  const { postId } = request.params;

  // 调取内容
  try {
    const data = await getIdNotification(parseInt(postId, 10));
    if (data) {
      let remark = await getInfoByIdAndType(data.target_id, data.target_type);
      data.remark = remark
    }

    // 做出响应
    response.send(data);
  } catch (error) {
    next(error);
  }
};

/**
 * 创建内容
 */
export const store = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {

  let noticePost = request.body;
  noticePost.status = 0;
  let date = Math.floor(new Date().getTime())
  noticePost.releaseTime = date;

  try {

    if (noticePost.id && noticePost.id > 0) {
      const data = await updateNotice(noticePost, parseInt(noticePost.id, 10));
      response.send(data);
    } else {
      const data = await createNotice(noticePost);
      response.send(data);
    }

  } catch (error) {
    next(error);
  }

}

/**
 * 阅读
 */
export const read = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {

  let notificationodal = request.body;
  let notification_id = notificationodal.notification_id
  let user_id = request.user.id;

  // 更新阅读
  const read = _.pick(request.body, ['read']);

  try {
    const data = await readNotification(1,parseInt(notification_id, 10), parseInt(`${user_id}`, 10));
    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 更新内容
 */
export const update = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取更新内容 ID
  const { postId } = request.params;

  // 更新内容
  const post = _.pick(request.body, [
    'status',
    'content' 
  ]);

  // 更新内容
  try {
    const data = await updateNotice(post, parseInt(postId, 10));
    response.send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 删除内容
 */
export const destroy = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取删除内容 ID
  const { postId } = request.body;

  // 删除内容
  try {
    const data = await deleteNotice(parseInt(postId, 10));

    response.send(data);
  } catch (error) {
    next(error)
  }
}

/**
 * 状态
 */
export const status = async (
  request: Request,
  response: Response,
  next: NextFunction,
) => {
  // 获取内容 ID
  const postId = request.body.id;

  // 获取内容
  const post = _.pick(request.body, ['status']);

  // 执行
  try {
    const data = await noticeStatusChange(parseInt(postId, 10), post);
    response.send(data);
  } catch (error) {
    next(error);
  }
};
